<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="赛事标题" prop="gameTitle">
        <el-input
          v-model="queryParams.gameTitle"
          placeholder="请输入赛事标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="赛事分类" prop="typeId">
        <treeselect style="width:200px" v-model="queryParams.typeId" :options="gametypeOptions" :normalizer="normalizer" placeholder="请选择分类" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['baseplatform:game:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['baseplatform:game:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['baseplatform:game:remove']"
        >删除</el-button>
      </el-col>
<!--      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['baseplatform:game:export']"
        >导出</el-button>
      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="gameList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="赛事分类" align="center" prop="typeName" />
      <el-table-column label="赛事标题" align="center" prop="gameTitle" />
      <el-table-column label="赛事描述" align="center" prop="gameDesc" />
      <el-table-column label="比赛日" align="center" prop="gameDate" />
      <el-table-column label="比赛开始时间" align="center" prop="gameStartTime" width="180">
      </el-table-column>
      <el-table-column label="报名开始日期" align="center" prop="applyStartTime" width="180">
      </el-table-column>
      <el-table-column label="报名截止日期" align="center" prop="applyEndTime" width="180">
      </el-table-column>
      <el-table-column label="活动地点" align="center" prop="placeName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['baseplatform:game:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['baseplatform:game:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改赛事对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="赛事分类" prop="typeId">
          <treeselect v-model="form.typeId" :options="gametypeOptions" :normalizer="normalizer" placeholder="请选择分类" />
        </el-form-item>
        <el-form-item label="赛事标题" prop="gameTitle">
          <el-input v-model="form.gameTitle" placeholder="请输入赛事标题" />
        </el-form-item>
        <el-form-item label="赛事描述" prop="gameDesc">
          <el-input v-model="form.gameDesc" placeholder="请输入赛事描述" />
        </el-form-item>
        <el-form-item label="比赛日" prop="gameDate">
          <el-date-picker clearable
                          v-model="form.gameDate"
                          type="dates"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择一个或多个比赛日">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="封面图" prop="gameImg">
          <image-upload v-model="form.gameImg" :limit="1"/>
        </el-form-item>
        <el-form-item label="比赛开始时间" prop="gameStartTime">
          <el-time-picker clearable
                          v-model="form.gameStartTime"
                          format="HH:mm:ss"
                          value-format="HH:mm:ss"
                          :picker-options="{selectableRange:'00:00:00-23:59:59'}"
                          placeholder="请选择比赛开始时间">
          </el-time-picker>
        </el-form-item>
        <el-form-item label="报名开始时间" prop="applyStartTime">
          <el-date-picker clearable
                          v-model="form.applyStartTime"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          placeholder="请选择报名开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="报名截止时间" prop="applyEndTime">
          <el-date-picker clearable
                          v-model="form.applyEndTime"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          placeholder="请选择报名截止时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="可报名席位" prop="totalSeat">
          <el-input-number v-model="form.totalSeat" controls-position="right" :min="0" />
<!--          <el-input v-model="form.totalSeat" placeholder="请输入可报名席位" />-->
        </el-form-item>
        <el-form-item label="实际参赛席位" prop="realSeat">
          <el-input-number v-model="form.realSeat" controls-position="right" :min="0" />
<!--          <el-input v-model="form.realSeat" placeholder="请输入实际参赛席位" />-->
        </el-form-item>
        <el-form-item label="参赛人数" prop="realPersonNum">
          <el-input-number v-model="form.realPersonNum" controls-position="right" :min="0" />
<!--          <el-input v-model="form.realPersonNum" placeholder="请输入参赛人数" />-->
        </el-form-item>
        <el-form-item label="候补人数" prop="candidateNum">
          <el-input-number v-model="form.candidateNum" controls-position="right" :min="0" />
<!--          <el-input v-model="form.candidateNum" placeholder="请输入候补人数" />-->
        </el-form-item>
        <el-form-item label="活动地点" prop="placeName">
          <el-input v-model="form.placeName" placeholder="请输入活动地点" />
        </el-form-item>
        <div style="color: red;">比赛活动地点定位</div>
        <div><a href="https://lbs.qq.com/getPoint/" target="_blank" style="color: blue; width: 100px;">点我取点</a>  例:38.145905,114.516479；38.145905是维度，114.516479是经度</div>
        <el-form-item label="经度" prop="longitude">
          <el-input v-model="form.longitude" placeholder="请输入经度" />
        </el-form-item>
        <el-form-item label="纬度" prop="latitude">
          <el-input v-model="form.latitude" placeholder="请输入纬度" />
        </el-form-item>
        <el-form-item label="报名成功的二维码" prop="contactImgPath">
          <image-upload v-model="form.contactImgPath" :limit="1"/>
        </el-form-item>
        <el-form-item label="报名费(元)" prop="entryFee">
          <!-- <el-input v-model="form.entryFee" placeholder="请输入报名费" />-->
          <el-input-number v-model="form.entryFee" placeholder="请输入报名费" controls-position="right" :min="0" :step="1" :precision="2"/>
        </el-form-item>
        <el-form-item label="报名费用描述" prop="entryFeeDesc">
          <el-input v-model="form.entryFeeDesc" placeholder="请输入报名费用描述" />
        </el-form-item>
        <el-form-item label="赛事协议" prop="gameAgreementId">
<!--          <el-input v-model="form.gameAgreementId" placeholder="请输入赛事协议id" />-->
          <el-select v-model="form.gameAgreementId" placeholder="请选择赛事协议">
            <el-option
              v-for="item in agreementOptions"
              :key="item.id"
              :label="item.agreementName"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="备注" prop="remark">
          <el-input type="textarea" :rows="3" v-model="form.remark"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listGame, getGame, delGame, addGame, updateGame } from "@/api/baseplatform/game";
import { listGametype } from "@/api/baseplatform/gametype";
import { listAgreement } from "@/api/baseplatform/agreement";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Game",
  dicts: ['sys_user_sex'],
  components: {
    Treeselect
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 赛事表格数据
      gameList: [],
      //赛事类型树
      gametypeOptions: [],
      //协议选项
      agreementOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        typeId: null,
        gameTitle: null,
        expireTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        typeId: [
          { required: true, message: "请选择赛事分类", trigger: "blur" }
        ],
        gameTitle: [
          { required: true, message: "请输入赛事标题", trigger: "blur" }
        ],
        gameDate: [
          { required: true, message: "请输入比赛日", trigger: "blur" }
        ],
        placeName: [
          { required: true, message: "请输入活动地点", trigger: "blur" }
        ],
        gameStartTime: [
          { required: true, message: "请输入比赛开始时间", trigger: "blur" }
        ],
        applyStartTime: [
          { required: true, message: "请输入报名开始日期", trigger: "blur" }
        ],
        applyEndTime: [
          { required: true, message: "请输入报名截止日期", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getTreeselect();
    this.getAgreementSelect();
  },
  methods: {
    /** 查询赛事列表 */
    getList() {
      this.loading = true;
      listGame(this.queryParams).then(response => {
        this.gameList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询分类下拉树结构 */
    getTreeselect() {
      listGametype().then(response => {
        this.gametypeOptions = [];
        const data = { id: 0, typeName: '顶级节点', children: [] };
        data.children = this.handleTree(response.data, "id", "parentId");
        this.gametypeOptions.push(data);
      });
    },
    /** 查询协议下拉框 */
    getAgreementSelect() {
      listAgreement().then(response => {
        this.agreementOptions = [];
        this.agreementOptions = response.rows;
      });
    },
    /** 转换文章管理-栏目数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.typeName,
        children: node.children
      };
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        gameTitle: null,
        typeId: null,
        gameImg: null,
        gameStartTime: null,
        applyStartTime: null,
        applyEndTime: null,
        placeName: null,
        sex: null,
        personNum: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        isDel: null,
        remark: null,
        gameDesc: null,
        totalSeat: null,
        realSeat: null,
        realPersonNum: null,
        candidateNum: null,
        gameStatus: null,
        longitude: null,
        latitude: null,
        contactImgPath: null,
        entryFee: null,
        entryFeeDesc: null,
        gameAgreementId: null,
        gameDate: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加赛事";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getGame(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改赛事";
      });
    },
    /** 提交按钮 */
    submitForm() {
      if(this.form.gameImg && this.form.gameImg instanceof Array){
        this.form.gameImg = this.form.gameImg[0].url;
      }
      if(this.form.contactImgPath && this.form.contactImgPath instanceof Array){
        this.form.contactImgPath = this.form.contactImgPath[0].url;
      }
      if(this.form.gameDate && this.form.gameDate instanceof Array){
        this.form.gameDate = this.form.gameDate.join(",");
      }
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateGame(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addGame(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除赛事编号为"' + ids + '"的数据项？').then(function() {
        return delGame(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('baseplatform/game/export', {
        ...this.queryParams
      }, `game_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
